package com.brite.android.game.action;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.Scene.IOnSceneTouchListener;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
import org.anddev.andengine.opengl.view.RenderSurfaceView;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import com.brite.android.game.menu.MainMenu;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;

import android.content.Intent;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.KeyEvent;
import android.widget.FrameLayout;

public class LoadStoryActivity extends BaseGameActivity {

	private int CAMERA_WIDTH = 480;
	private int CAMERA_HEIGHT = 320;
	
	private Scene scene;
	
	private Texture bgTexture;
	private TextureRegion bgTextureRegion;
	
	private TimeCounter timeToActive = new TimeCounter(2.5f);
	
//	private Texture childrenTexture;
//	private TiledTextureRegion childrenTextureRegion;
	
	@Override
	public Engine onLoadEngine() {
		Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		EngineOptions eo = new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
		eo.setNeedsMusic(true);
		eo.setNeedsSound(true);
		mEngine = new Engine(eo);
		
		return mEngine;
	}

	@Override
	public void onLoadResources() {
		TextureRegionFactory.setAssetBasePath("gfx/");
		
		bgTexture = new Texture(1024, 1024, TextureOptions.DEFAULT);
		bgTextureRegion = TextureRegionFactory.createFromAsset(bgTexture, this, "menu-game-3.png", 0, 0);

//		childrenTexture = new Texture(512, 128, TextureOptions.DEFAULT);
//		childrenTextureRegion = TextureRegionFactory.createTiledFromAsset(childrenTexture, this, "select_chidren(100x180).png", 0, 0, 2, 1);
		
		mEngine.getTextureManager().loadTextures(bgTexture);
	}

	@Override
	public Scene onLoadScene() {
		scene = new Scene(1);
		
		Sprite bgSprite = new Sprite(0, 0, bgTextureRegion);
		bgSprite.setWidth(CAMERA_WIDTH);
		bgSprite.setHeight(CAMERA_HEIGHT);
		scene.attachChild(bgSprite);
		
//		final AnimatedSprite childrenAnimateSprite = new AnimatedSprite(0, 0, childrenTextureRegion);
//		childrenAnimateSprite.setPosition(CAMERA_WIDTH - (CAMERA_WIDTH/4 - childrenAnimateSprite.getWidthScaled())/2, CAMERA_HEIGHT/2 + (CAMERA_HEIGHT/4 - childrenAnimateSprite.getHeightScaled())/2);
//		
//		scene.attachChild(childrenAnimateSprite);
		
		scene.registerUpdateHandler(new IUpdateHandler() {
			
			@Override
			public void reset() {
				
			}
			
			@Override
			public void onUpdate(float pSecondsElapsed) {
				if (!timeToActive.currentStatus()) {
					startActivity(new Intent(getBaseContext(), ChoseCharacterAndMapActivity.class));
					finish();
				}else {
					timeToActive.update(pSecondsElapsed);
				}
			}
		});
		
		scene.setOnSceneTouchListener(new IOnSceneTouchListener() {
			
			@Override
			public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
				startActivity(new Intent(getBaseContext(), ChoseCharacterAndMapActivity.class));
				finish();
				return true;
			}
		});
		
		return scene;
	}

	@Override
	public void onLoadComplete() {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			startActivity(new Intent(this, MainMenu.class));
			finish();
		}
		return super.onKeyDown(keyCode, event);
	}
	
	@Override
    protected void onSetContentView() {
        final FrameLayout frameLayout = new FrameLayout(this);
        final FrameLayout.LayoutParams frameLayoutLayoutParams =
                new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT,
                                             FrameLayout.LayoutParams.FILL_PARENT);
 
        final AdView adView = new AdView(this, AdSize.IAB_BANNER, "a14fbdf32995e3c");
        adView.refreshDrawableState();
        adView.setVisibility(AdView.VISIBLE);
        final FrameLayout.LayoutParams adViewLayoutParams =
                new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
                                             FrameLayout.LayoutParams.WRAP_CONTENT,
                                             Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
        // getHeight returns 0
        // http://groups.google.com/group/admob-pu ... a874df3472
        int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50,
                getResources ().getDisplayMetrics ());
        // top of AD is at middle of the screen
//        adViewLayoutParams.topMargin = height/2;
//        adViewLayoutParams.topMargin = 0;
        Log.v("Add H", "Adview height : " + height);
        Log.v("Add S", "Adview size {x:" + adView.getWidth() + ", y: " + adView.getHeight()+ "}");
 
        AdRequest adRequest = new AdRequest();
        adRequest.setTesting(true);
        adView.loadAd(adRequest);
        if (adView.isReady()) {
        	Log.v("Add S", "Adview size {x:" + adView.getWidth() + ", y: " + adView.getHeight()+ "}");
		}
        adView.setPadding(0, 0, 0, CAMERA_HEIGHT - adView.getHeight() - 60);
        
        this.mRenderSurfaceView = new RenderSurfaceView(this);
        mRenderSurfaceView.setRenderer(mEngine);
 
        final android.widget.FrameLayout.LayoutParams surfaceViewLayoutParams =
                new FrameLayout.LayoutParams(super.createSurfaceViewLayoutParams());
 
        frameLayout.addView(this.mRenderSurfaceView, surfaceViewLayoutParams);
        frameLayout.addView(adView, adViewLayoutParams);
 
        this.setContentView(frameLayout, frameLayoutLayoutParams);
    }
}
